package com.bupt.uchouten.common.register;

import com.bupt.uchouten.common.item.SwordOfScarletItem;
import com.bupt.uchouten.common.util.Reference;
import com.google.common.collect.Lists;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.List;
import java.util.function.Supplier;

public final class UchoutenItems {
    public static final DeferredRegister<Item> REGISTER = DeferredRegister.create(ForgeRegistries.ITEMS, Reference.MODID);

    public static final ItemEntry<Item> KEYSTONE = ItemEntry.registerSeparately("keystone", () -> new Item(new Item.Properties()));

    public static final class Foods {

        public static final ItemEntry<Item> TENKAI_FRUIT = ItemEntry.register(
                "tenkai_fruit", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
                        .nutrition(6)
                        .saturationMod(10.0f)
                        .build())
                )
        );

        private Foods() {
        }

        public static void init() {
        }
    }

    public static final class Weapons {
        public static final ItemEntry<SwordOfScarletItem> SWORD_OF_SCARLET_PERCEPTION = ItemEntry.register(
                "sword_of_scarlet_perception", SwordOfScarletItem::new);


        private Weapons() {
        }

        public static void init() {
        }
    }

    private UchoutenItems() {
    }

    public static void init(IEventBus bus) {
        REGISTER.register(bus);

        Foods.init();
        Weapons.init();
    }

    public static class ItemEntry<T extends Item> implements Supplier<T>, ItemLike {

        public static final List<ItemEntry<? extends Item>> ALL_ITEMS = Lists.newArrayList();

        private final RegistryObject<T> regObject;

        private ItemEntry(RegistryObject<T> regObject, boolean addToCreativeTab) {
            this.regObject = regObject;
            if (addToCreativeTab){
                ALL_ITEMS.add(this);
            }
        }

        public static <T extends Item> ItemEntry<T> register(String name, Supplier<? extends T> make) {
            return new ItemEntry<>(REGISTER.register(name, make), true);
        }

        public static <T extends Item> ItemEntry<T> registerSeparately(String name, Supplier<? extends T> make) {
            return new ItemEntry<>(REGISTER.register(name, make), false);
        }

        @Override
        public T get() {
            return this.regObject.get();
        }

        @Override
        public Item asItem() {
            return this.regObject.get();
        }

        public ResourceLocation getId() {
            return this.regObject.getId();
        }
    }
}
